if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

function root_page(page_url){
	window.open(page_url, '_self');
}

function hand_login(e){
	if (e.keyCode == 13) {
		do_login();
		return false;
	}else{
		return true;
	}
}

function do_login(){
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	var remember = document.getElementById("remember").checked;
	
	if(username.length > 0 && trim(username) != ""){
		var post_param = "username=" + username + "&password=" + password + "&remember=" + remember;
		
		xmlhttp.open("POST","do_login.php",true);
	
		//Send the proper header information along with the request
	
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	
		xmlhttp.setRequestHeader("Content-length", post_param.length);
	
		xmlhttp.setRequestHeader("Connection", "close");
	
		xmlhttp.onreadystatechange = function() {//Call a function when the state changes.
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				
				if(xmlhttp.responseText == "Successful."){
					
					window.open('index.php', '_self');
					//document.getElementById("error_message").innerHTML = xmlhttp.responseText;
				}else{
					document.getElementById("password").value = "";
					document.getElementById("error").innerHTML = xmlhttp.responseText;
				}
			}
		}
	
		xmlhttp.send(post_param);
	}else{
		alert('Username cannot be blank');
	}
	
}

function do_change_true_false(table, id, field){
	var alt = document.getElementById(field+id).alt;
	if (alt == "true") {
		var value = 0;
		xmlhttp.open("GET","do_change_true_false.php?table=" + table + "&id=" + id + "&field=" + field + "&value=" + value, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				xmlhttp.getResponseHeader("Content-Type");
			}
		};
		xmlhttp.send(null);
		document.getElementById(field+id).alt = "false";
		document.getElementById(field+id).src = "imgs/anhien_0.jpg";
	}
	if (alt == "false") {
		var value = 1;
		xmlhttp.open("GET","do_change_true_false.php?table=" + table + "&id=" + id + "&field=" + field + "&value=" + value, true);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				xmlhttp.getResponseHeader("Content-Type");
			}
		};
		xmlhttp.send(null);
		document.getElementById(field+id).alt = "true";
		document.getElementById(field+id).src = "imgs/anhien_1.jpg";
	}
}

function do_delete_record(table, id){
	xmlhttp.open("GET","do_delete_record.php?table=" + table + "&id=" + id,true);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			window.location.reload();
		}
	};
	xmlhttp.send(null);
}

function create_record(p){
	window.open('index.php?p=' + p, '_self');
}

function update_record(p, id){
	window.open('index.php?p=' + p + '&id=' + id, '_self');
}

function do_move_record(table, id, loai_move){
	xmlhttp.open("GET","do_move_record.php?table=" + table + "&id=" + id + "&loai_move=" + loai_move,true);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			window.location.reload();
		}
	};
	xmlhttp.send(null);
}

function get_chil_record(id_chil,table,table_parent,field,idparent){
	xmlhttp.open("GET","do_get_chil_record.php?table=" + table + "&table_parent=" + table_parent + "&field=" + field + "&idparent=" + idparent,true);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("idtl").innerHTML = "<option value='1'>khanh</option>";
		}
	};
	xmlhttp.send(null);
}

function check_username(){
	var username = document.getElementById("username").value;
	xmlhttp.open("GET","check_username.php?username=" + username,true);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("username_result").innerHTML = xmlhttp.responseText;
		}
	};
	xmlhttp.send(null);
}

















function trim(str){
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function replaceAll(Source, stringToFind, stringToReplace){

	var temp = Source;
	
	var index = temp.indexOf(stringToFind);
	
	while(index != -1){
	
		temp = temp.replace(stringToFind,stringToReplace);
		
		index = temp.indexOf(stringToFind);
		
	}
	
	return temp;

}

